package com.ruoyi.aftersale.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.aftersale.domain.DmsTechnicalSupport;
import com.ruoyi.aftersale.mapper.DmsTechnicalSupportMapper;
import com.ruoyi.aftersale.service.IDmsTechnicalSupportService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AutofillUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 技术支援管理Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-16
 */
@Service
public class DmsTechnicalSupportServiceImpl extends ServiceImpl<DmsTechnicalSupportMapper,DmsTechnicalSupport> implements IDmsTechnicalSupportService
{
    @Autowired
    private DmsTechnicalSupportMapper dmsTechnicalSupportMapper;
    @Autowired
    private AutofillUtil autofillUtil;
    @Autowired
    private ISysDeptService iSysDeptService;

    /**
     * 查询技术支援管理
     * 
     * @param id 技术支援管理主键
     * @return 技术支援管理
     */
    @Override
    public DmsTechnicalSupport selectDmsTechnicalSupportById(Long id)
    {
        return dmsTechnicalSupportMapper.selectDmsTechnicalSupportById(id);
    }

    /**
     * 查询技术支援管理列表
     * 
     * @param dmsTechnicalSupport 技术支援管理
     * @return 技术支援管理
     */
    @Override
    public List<DmsTechnicalSupport> selectDmsTechnicalSupportList(DmsTechnicalSupport dmsTechnicalSupport)
    {
        return dmsTechnicalSupportMapper.selectDmsTechnicalSupportList(dmsTechnicalSupport);
    }

    /**
     * 新增技术支援管理
     * 
     * @param dmsTechnicalSupport 技术支援管理
     * @return 结果
     */
    @Override
    public int insertDmsTechnicalSupport(DmsTechnicalSupport dmsTechnicalSupport)
    {
        //新增时状态只能为已上报待回复
        if (!dmsTechnicalSupport.getStatus().equals(1)){
            throw new ServiceException("新增时状态只能为已提交待回复！");
        }

        //如果未填技术支持代码，则生成一个
        if (StrUtil.isBlank(dmsTechnicalSupport.getTechnicalSupportCode())){
            dmsTechnicalSupport.setTechnicalSupportCode(IdUtils.fastSimpleUUID());
        }

        //新增自动填充
        autofillUtil.autoFillCreate(dmsTechnicalSupport);
        //新增记录上报人
        dmsTechnicalSupport.setReportBy(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsTechnicalSupport.setReportByName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsTechnicalSupport.setReportAt(new Date());
        //填充经销商id 名称
        dmsTechnicalSupport.setDealerId(SecurityUtils.getLoginUser().getUser().getDept().getParentId());
        dmsTechnicalSupport.setDealerName(iSysDeptService.selectDeptById(SecurityUtils.getLoginUser().getUser().getDept().getParentId()).getDeptName());
        return dmsTechnicalSupportMapper.insertDmsTechnicalSupport(dmsTechnicalSupport);
    }

    /**
     * 修改技术支援管理
     * 
     * @param dmsTechnicalSupport 技术支援管理
     * @return 结果
     */
    @Override
    public int updateDmsTechnicalSupport(DmsTechnicalSupport dmsTechnicalSupport)
    {
        //状态不可被扭转，已回复待完结时可以回到前一个状态，因为可能问题还未解决
        Integer status = dmsTechnicalSupport.getStatus();
        DmsTechnicalSupport technicalSupportDB = selectDmsTechnicalSupportById(dmsTechnicalSupport.getId());
        Integer statusDB = technicalSupportDB.getStatus();
        if (!statusDB.equals(2) && status-statusDB != 1){
            throw new ServiceException("状态不可逆转 和 跨越！");
        }
        if (status.equals(statusDB)){
            throw new ServiceException("每次审核必须修改状态！");
        }

        //修改自动填充
        autofillUtil.autoFillUpdate(dmsTechnicalSupport);
        //填充审核人
        dmsTechnicalSupport.setAuditorId(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsTechnicalSupport.setAuditorName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsTechnicalSupport.setAuditorTime(new Date());
        //根据状态填充 上报人、回复人、完结人
        if (dmsTechnicalSupport.getStatus().equals(2)){
            dmsTechnicalSupport.setReplyBy(SecurityUtils.getLoginUser().getUser().getUserId());
            dmsTechnicalSupport.setReplyByName(SecurityUtils.getLoginUser().getUser().getUserName());
            dmsTechnicalSupport.setReplyAt(new Date());
        }
        else if (dmsTechnicalSupport.getStatus().equals(3)){
            dmsTechnicalSupport.setFinishBy(SecurityUtils.getLoginUser().getUser().getUserId());
            dmsTechnicalSupport.setFinishByName(SecurityUtils.getLoginUser().getUser().getUserName());
            dmsTechnicalSupport.setFinishAt(new Date());
        }
        return dmsTechnicalSupportMapper.updateDmsTechnicalSupport(dmsTechnicalSupport);
    }

    /**
     * 批量删除技术支援管理
     * 
     * @param ids 需要删除的技术支援管理主键
     * @return 结果
     */
    @Override
    public int deleteDmsTechnicalSupportByIds(Long[] ids)
    {
        return dmsTechnicalSupportMapper.deleteDmsTechnicalSupportByIds(ids);
    }

    /**
     * 删除技术支援管理信息
     * 
     * @param id 技术支援管理主键
     * @return 结果
     */
    @Override
    public int deleteDmsTechnicalSupportById(Long id)
    {
        return dmsTechnicalSupportMapper.deleteDmsTechnicalSupportById(id);
    }
}
